package avicit.bdp.dms.tdm.rest;

import avicit.bdp.dms.tdm.dto.ProcessApproveDTO;
import avicit.bdp.dms.tdm.service.ProcessApproveService;
import avicit.platform6.api.bpm.dto.ProcessParameter;
import avicit.platform6.api.bpm.eventInterface.CommonRestInstanceDeleteEventListener;
import avicit.platform6.bpmreform.bpmbusiness.dto.StartResultBean;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：${tableInfo.classCreator}
 * @邮箱：${tableInfo.classMailbox}
 * @创建时间： 2023-11-14 16:03
 * @类说明：bdp_asset_approveRest
 * @修改记录：
 */
@RestController
@Api(tags = "流程审核")
@RequestMapping("/api/bdp/dms/process/approve")
public class ProcessApproveRest implements CommonRestInstanceDeleteEventListener {

    private static final Logger logger = LoggerFactory.getLogger(ProcessApproveRest.class);
    @Autowired
    private ProcessApproveService processApproveService;

    @ApiOperation(value = "我的审核-按条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "业务标题", name = "businessTitle", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "流程类型", name = "flowType", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(value = "bpmType", name = "bpmType", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "bpmState", name = "bpmState", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
    })
    @GetMapping("/getMyProcessPageList")
    public ResponseMsg<QueryRespBean<ProcessApproveDTO>> getMyProcessPageList(
            @RequestParam(required = false) String businessTitle,
            @RequestParam(required = false) Integer flowType,
            @RequestParam(required = false) String bpmType,
            @RequestParam(required = false) String bpmState,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        ResponseMsg<QueryRespBean<ProcessApproveDTO>> responseMsg = new ResponseMsg<>();
        QueryRespBean<ProcessApproveDTO> queryRespBean = processApproveService.getMyProcessPageList(businessTitle, flowType, bpmType, bpmState, pageNo, pageSize);
        responseMsg.setResponseBody(queryRespBean);
        return responseMsg;
    }

    @ApiOperation(value = "我的申请-按条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "资产名称", name = "keyWord", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "资源类型", name = "resourceType", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(value = "bpmType", name = "bpmType", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "bpmState", name = "bpmState", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "开始时间", name = "applyTimeBegin", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "结束时间", name = "applyTimeEnd", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
    })
    @GetMapping("/getMyApplyPageList")
    public ResponseMsg<QueryRespBean<ProcessApproveDTO>> getMyApplyPageList(
            @RequestParam(required = false) String keyWord,
            @RequestParam(required = false) Integer resourceType,
            @RequestParam(required = false) String bpmType,
            @RequestParam(required = false) String bpmState,
            @RequestParam(required = false) String applyTimeBegin,
            @RequestParam(required = false) String applyTimeEnd,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        ResponseMsg<QueryRespBean<ProcessApproveDTO>> responseMsg = new ResponseMsg<>();
        QueryRespBean<ProcessApproveDTO> queryRespBean = processApproveService.getMyApplyPageList(keyWord, resourceType,
                applyTimeBegin, applyTimeEnd, bpmType, bpmState, pageNo, pageSize);
        responseMsg.setResponseBody(queryRespBean);
        return responseMsg;
    }

    /**
     * 新增并启动流程
     *
     * @param processParam 参数对象
     * @return ResponseMsg<StartResultBean>
     */
    @ApiOperation(value = "新增并启动流程")
    @PostMapping("/saveAndStartProcess")
    public ResponseMsg<StartResultBean> saveAndStartProcess(@ApiParam(value = "参数对象", name = "processParam") @RequestBody ProcessParameter<ProcessApproveDTO> processParam) {
        ResponseMsg<StartResultBean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(processApproveService.saveAndStartProcess(processParam));
        return responseMsg;
    }

    /**
     * 批量删除
     *
     * @param ids 逗号分隔的id串
     */
    @ApiOperation(value = "批量删除")
    @DeleteMapping("/deleteByIds")
    public ResponseMsg<Boolean> deleteByIds(@ApiParam(value = "id数组", name = "ids") @RequestBody String ids) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        processApproveService.deleteByIds(ids);
        responseMsg.setResponseBody(true);
        return responseMsg;
    }

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public ResponseMsg<String> save(@RequestBody ProcessApproveDTO dto) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(processApproveService.save(dto));
        return responseMsg;
    }

    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public ResponseMsg<Boolean> update(@RequestBody ProcessApproveDTO dto) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        processApproveService.update(dto);
        responseMsg.setResponseBody(true);
        return responseMsg;
    }

    @ApiOperation(value = "通过ID查询")
    @ApiParam(value = "id", name = "id", required = true)
    @GetMapping("/{id}")
    public ResponseMsg<ProcessApproveDTO> queryById(@PathVariable String id) {
        ResponseMsg<ProcessApproveDTO> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(processApproveService.queryById(id));
        return responseMsg;
    }

    /**
     * 流程实例删除回调方法（按主键单条删除业务数据）
     *
     * @param arg 流程变量集合
     */
    @Override
    @PostMapping("/notify/v1")
    @ApiOperation(value = "流程实例删除回调方法（按主键单条删除业务数据）")
    public ResponseMsg<Void> notify(
            @ApiParam(value = "流程变量集合", name = "arg") @RequestBody Map<String, String> arg) {
        ResponseMsg<Void> responseMsg = new ResponseMsg<>();
        String formId = arg.get("formId");
        processApproveService.deleteById(formId);
        return responseMsg;
    }

}